<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{
	//var $per_page;

	public function get_access_rule()
	{
		$rule_action["rule_type"] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		
		$rule_action['actions'] = array(
			'index',
		);
		
		return $rule_action;
	}

	public function setup()
	{
		$this->crumb('问题', '/question/');
	}

	public function index_action()
	{
		/*if (get_setting('contents_per_page'))
		{
			$this->per_page = get_setting('contents_per_page');
		}*/
		
		if(!isset($_GET['id']))
		{
			HTTP::redirect('/home/explore/');
		}
		
		$answer_ids = array();
		$question_id = intval($_GET['id']);
		
		if ($question_id == 0)
		{
			H::redirect_msg('问题不存在或已被删除', '/home/explore/');
		}
		
		if (!$_GET['sort'] OR $_GET['sort'] != 'ASC')
		{
			$_GET['sort'] = 'DESC';
		}
		else
		{
			$_GET['sort'] = 'ASC';
		}
		
		if ($_GET['notification_id'])
		{
			$this->model('notify')->read_notification($_GET['notification_id']);
		}
		
		$question_info = $this->model("question")->get_question_info_by_id($question_id);
			
		if (!$question_info)
		{
			H::redirect_msg('问题不存在或已被删除', '/home/explore/');
		}
		
		$question_visit_count = $question_info['view_count'];
		
		$question_info['view_count'] = $question_visit_count;
		
		$this->model('question')->increament_question_visist_count($question_id);
		
		$question_info['question_detail'] = nl2br(strip_tags($question_info['question_detail']));
		$question_info['question_detail'] = FORMAT::parse_links($question_info['question_detail']);
		
		$question_info['attachs'] = $this->data_cache("question_attach_" . $question_info['question_id'], '$this->model("question")->get_question_attach(' . $question_info['question_id'] . ', \'min\')', get_setting('cache_level_high'), "question_detail_" . $question_id);	// 附件
		
		if ($question_info['category_id'])
		{
			$question_info['category_info'] = $this->model('system')->get_category_info($question_info['category_id']); 
		}

		$question_topics = $this->data_cache("question_topics_" . $question_id, '$this->model("question_topic")->get_question_topic_by_question_id(' . $question_id . ')', get_setting('cache_level_high'), "question_detail_" . $question_id);
		
		$question_focus = $this->data_cache("question_focus_q" . $question_id . "_u" . $this->user_id, '$this->model("question")->has_focus_question(' . $question_id . ', ' . $this->user_id . ')', get_setting('cache_level_high'), "question_detail_" . $question_id);
		
		$question_relike_list = $this->data_cache("question_relike_list_q" . $question_id . "_u" . $this->user_id, '$this->model("question")->get_relike_question_list(' . $question_id . ')', get_setting('cache_level_high'), "question_detail_" . $question_id);
		
		$question_info['user_info'] = $this->data_cache("account_get_users_" . $question_info['published_uid'], '$this->model("account")->get_users(' . $question_info['published_uid'] . ',true)', get_setting('cache_level_high'), "question_detail_" . $question_id);
		
		if ($_GET['column'] != 'log')
		{
			if (is_numeric($_GET['uid']))
			{
				$answer_list_where[] = 'an.uid = ' . intval($_GET['uid']);
				
				$answer_count_where = ' uid = ' . intval($_GET['uid']);
			}
			else if ($_GET['uid'] == 'focus' AND $this->user_id)
			{
				if ($friends = $this->data_cache(ZCACHE::format_key("user_friends_".$this->user_id), '$this->model("follow")->get_user_friends("'.$this->user_id.'", false)', $this->setting['cache_level_high'], "g_uid_".$this->user_id))
				{
					foreach ($friends AS $key => $val)
					{
						$follow_uids[] = $val['friend_uid'];
					}
				}
				else
				{
					$follow_uids[] = 0;	
				}
				
				$answer_list_where[] = 'an.uid IN(' . implode($follow_uids, ',') . ')';
				$answer_count_where = ' uid IN(' . implode($follow_uids, ',') . ')';
			}
			else if ($_GET['sort_key'] == 'add_time')
			{
				$answer_order_by = $_GET['sort_key'] . " " . $_GET['sort'];
			}
			else
			{
				$answer_order_by = "agree_count " . $_GET['sort'];
				$answer_order_by .= ",add_time " . $_GET['sort'];
			}
			
			$answer_count = $this->model("answer")->get_answer_count_by_question_id($question_id , $answer_count_where);
			
			if (! $this->user_id AND $_GET['source'])
			{
				HTTP::set_cookie('source', $_GET['source']);
			}
			
			if (isset($_GET['answer_id']) and ! $this->user_id)
			{			
				$answer_list = $this->model('answer')->get_answer_list_by_question_id($question_id, 1, 'AND an.answer_id = ' . (int)$_GET['answer_id']);
			}
			else if (! $this->user_id)
			{
				if ($question_info['best_answer'])
				{
					$answer_list = $this->model('answer')->get_answer_list_by_question_id($question_id, 1, 'AND an.answer_id = ' . (int)$question_info['best_answer']);
				}
				else
				{
					$answer_list = $this->model('answer')->get_answer_list_by_question_id($question_id, 1, null, 'agree_count DESC');
				}
			}
			else
			{
				//$limit = calc_page_limit($_GET['page'], $this->per_page);
				
				if ($answer_list_where)
				{
					$answer_list_where = ' AND ' . implode(' AND ', $answer_list_where);
				}
				
				$answer_list = $this->model('answer')->get_answer_list_by_question_id($question_id, $limit, $answer_list_where, $answer_order_by);
			}
			
			//p($answer_list);die;
			
			$answer_uids = array();
			
			$answers[0] = '';	// 预留给最佳答案
			
			if (!is_array($answer_list))
			{
				$answer_list=array();
			}
			foreach ($answer_list as $answer)
			{
				$answer_ids[] = $answer['answer_id'];
				$answer_uids[] = $answer['uid'];
				$answer['attachs'] = $this->model("answer")->get_answer_attach($answer['answer_id'], 'min');
				$answer['user_rated_thanks'] = $this->model("answer")->user_rated('thanks', $answer['answer_id'], $this->user_id);
				$answer['user_rated_uninterested'] = $this->model("answer")->user_rated('uninterested', $answer['answer_id'], $this->user_id);
				
				if ($answer['answer_content'])
				{
					//$answer_list_content = FORMAT::format_content($answer['answer_content']);
					
					//$answer['answer_title'] = strip_tags($answer_list_content['content_title']);
					
					$answer['answer_content'] = nl2br(strip_tags($answer['answer_content']));
					$answer['answer_content'] = FORMAT::parse_links($answer['answer_content']);
				}
				
				if ($answer['agree_count'] > 0)
				{
					$answer['agree_users'] = $this->model("answer")->get_vote_user_by_answer_id($answer['answer_id']);
				}
				
				$answer_vote = $this->model("answer")->get_answer_vote_status($answer['answer_id'], $this->user_id);
				$answer['agree_status'] = intval($answer_vote['vote_value']);
				
				if ($question_info['best_answer'] == $answer['answer_id'])
				{
					$answers[0] = $answer;
				}
				else
				{
					$answers[] = $answer;
				}
			}
			
			if (!$answers[0])
			{
				unset($answers[0]);
			}
			
			if (in_array($this->user_id, $answer_uids) && get_setting('answer_unique') == 'Y')
			{
				TPL::assign('user_answered', TRUE);
			}
			
			TPL::assign("answers", $answers);
			TPL::assign("answer_count", $answer_count);
		}
		
		if ($this->user_id)
		{
			TPL::assign('invite_users', $this->model('question')->get_invite_users($question_id, $this->user_id));
				
			TPL::assign('user_follow_check', $this->model("follow")->user_follow_check($this->user_id , $question_info['published_uid']));
		}
		
		TPL::assign("bind_weibo_qq", $this->data_cache("weibo_qq_" . $this->user_id, '$this->model("qq_weibo")->get_users_qq_by_uid(' . $this->user_id . ')', get_setting('cache_level_high'), "question_detail_" . $question_id));		
		TPL::assign("bind_weibo_sina", $this->data_cache("weibo_sina_" . $this->user_id, '$this->model("sina_weibo")->get_users_sina_by_uid(' . $this->user_id . ')', get_setting('cache_level_high'), "question_detail_" . $question_id));
			
		TPL::assign("question_id", $question_id);
		TPL::assign("question_focus", $question_focus);
		
		TPL::assign("question_topics", $question_topics);
		TPL::assign("question_info", $question_info);
		TPL::assign("question_relike_list", $question_relike_list);
		
		$user_job = $this->data_cache("user_job_" . $question_info['user_info']['uid'], '$this->model("account")->get_user_jobs_by_uids(' . $question_info['user_info']['uid'] . ')', get_setting('cache_level_high'), "question_detail_" . $question_id);
		
		TPL::assign("user_job", $user_job[$question_info['user_info']['uid']]);
		
		$this->crumb($question_info['question_content'], '/question/' . $question_id);
		
		if ($_GET['column'] == 'log')
		{
			$this->crumb('日志', '/question/id-' . $question_id . '__column-log');
		}
		else
		{
			TPL::assign('human_valid', human_valid('answer_valid_hour'));
		}
		
		TPL::set_meta('description', $question_info['user_info']['user_name'] . ': ' . $question_info['question_content'] . ' - ' . cjk_substr(str_replace("\r\n", ' ', strip_tags($question_info['question_detail'])), 128));
		
		TPL::import_css('css/discussion.css');
		TPL::import_js('js/question_detail.js');
		
		TPL::assign('attach_access_key', md5($this->user_id . time()));
		
		TPL::output("question/detail");
	}	

	function remove_question()
	{
		
		
		
		$question_info = $this->model('question')->get_question_info_by_id(intval($_GET['question_id']));
	
		if(!$question_info)
		{
			H::redirect_msg('问题不存在', '/');
		}
	
		
		if(($question_info['published_uid'] != $this->user_id) && (!$this->user_info['permission']['is_administortar'])&& (!$this->user_info['permission']['is_moderator']))
		{
			H::redirect_msg('对不起，你没有删除权限。', '/');
		}
	
		if($this->model('question')->remove_question($_GET['question_id']))
		{
			H::redirect_msg('删除问题成功', '/');
		}
		else
		{
			H::redirect_msg('删除失败，无法操作数据库。', '/');
		}
	}
}